package com.bys.controller;

import com.bys.entity.OrderPay;
import com.bys.util.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class PayController {
    @Value("${server.port}")
    private String serverPort;
    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public PayController() {
    }

    @GetMapping("/pay/string/getId/{payId}")
    public String getStringGetId(@PathVariable Integer payId){
        Object data = stringRedisTemplate.boundValueOps(payId.toString()).get();
        return String.valueOf(data) + "端口为： "+ serverPort;
    }
    @GetMapping("/pay/getId/{payId}")
    public Result getOrderPayById(@PathVariable Integer payId) throws JsonProcessingException {
        Object data = stringRedisTemplate.boundValueOps(payId.toString()).get();
        OrderPay orderPay = new ObjectMapper().readValue(String.valueOf(data), OrderPay.class);
        System.out.println(orderPay);
        return Result.success(orderPay);
    }
    @PostMapping("/pay/create")
    public Result createOrderPay(@RequestBody OrderPay orderPay) throws JsonProcessingException {
        stringRedisTemplate.boundValueOps(orderPay.getPayId().toString()).set(new ObjectMapper().writeValueAsString(orderPay));
        return Result.success();
    }

}
